/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

import { services } from '@/config'
import { checkEntityExist } from '@/module/admin/actusertaskconfig/api.js'

const validateModelKey = (rule, value, callback) => {
  checkEntityExist(value).then(response => {
    if (window.boxType === 'edit') {
      return callback()
    }
    const result = response.data.data
    if (result) {
      callback(new Error('流程编码已存在'))
    } else {
      callback()
    }
  })
}

export const tableOptionTodo = {
  border: true,
  index: true,
  indexLabel: '序号',
  stripe: true,
  menuAlign: 'center',
  searchMenuSpan: 6,
  editBtn: false,
  delBtn: false,
  align: 'center',
  addBtn: false,
  menu: false,
  column: [
    {
      fixed: true,
      label: 'id',
      span: 24,
      hide: true,
      editDisabled: true,
      addDisplay: false
    },
    {
      fixed: true,
      label: '状态',
      prop: 'processStatusName',
      editDisabled: true,
      slot: true,
      span: 24,
      width: 80
    },
    {
      label: '流程标题',
      prop: 'formName',
      value: '',
      span: 24,
      slot: true
    },
    {
      label: '环节名称',
      prop: 'name',
      value: '',
      width: 160,
      span: 24
    },
    {
      label: '提交人',
      prop: 'startPersonName',
      width: 70,
      span: 24
    },
    {
      label: '提交时间',
      prop: 'startTime',
      value: '',
      width: 135,
      span: 24
    },
    {
      label: '总耗时',
      prop: 'totalTime',
      width: 120,
      span: 24
    },
    {
      label: '关键字',
      prop: 'keyword',
      width: 150,
      span: 24,
      hide: true,
      search: true,
      display: false,
      searchPlaceholder: '请输入名称/编码'
    },
    {
      type: 'date',
      label: '时间范围',
      prop: 'dateScope',
      width: 150,
      span: 24,
      format: 'yyyy-MM-dd HH:mm:ss',
      valueFormat: 'yyyy-MM-dd HH:mm:ss',
      searchRange: true,
      display: false,
      hide: true,
      search: true
    },
    {
      type: 'select',
      label: '所属系统',
      prop: 'appName',
      width: 150,
      span: 24,
      // dicUrl: services.adminService+'workItem/getApps',
      dicMethod: 'get',
      props: { label: 'name', value: 'name' }
    }
  ]
}

export const treeOption = {
  title: '我是标题',
  filterText: '搜索关键字自定义',
  defaultExpandAll: true,
  addBtn: false,
  formOption: {
    labelWidth: 100,
    column: [
      {
        label: '自定义项',
        prop: 'label'
      }
    ]
  },
  props: {
    labelText: '标题',
    label: 'name',
    value: 'id',
    children: 'children'
  }
}

export const myDoneTableSchema = {
  border: true,
  index: true,
  indexLabel: '序号',
  stripe: true,
  menuAlign: 'center',
  searchMenuSpan: 6,
  editBtn: false,
  delBtn: false,
  align: 'center',
  addBtn: false,
  menu: false,
  column: [
    {
      fixed: true,
      label: 'id',
      span: 24,
      hide: true,
      editDisabled: true,
      addDisplay: false
    },
    {
      fixed: true,
      label: '状态',
      prop: 'processStatusName',
      editDisabled: true,
      slot: true,
      width: 80,
      span: 24
    },
    {
      label: '流程标题',
      prop: 'formName',
      value: '',
      span: 24
    },
    {
      label: '环节名称',
      prop: 'name',
      value: '',
      width: 160,
      span: 24
    },
    {
      label: '处理人',
      prop: 'startPersonName',
      width: 70,
      span: 24
    },
    {
      label: '处理时间',
      prop: 'createTime',
      value: '',
      span: 24,
      width: 135
    },
    {
      label: '处理意见',
      prop: 'message',
      value: '',
      span: 24,
      width: 160
    },
    {
      label: '总耗时',
      prop: 'totalTime',
      width: 150,
      span: 24
    },
    {
      label: '关键字',
      prop: 'keyword',
      width: 150,
      span: 24,
      search: true,
      display: false,
      hide: true,
      searchPlaceholder: '请输入名称/编码'
    },
    {
      type: 'date',
      label: '时间范围',
      prop: 'dateScope',
      width: 150,
      span: 24,
      format: 'yyyy-MM-dd HH:mm:ss',
      valueFormat: 'yyyy-MM-dd HH:mm:ss',
      searchRange: true,
      display: false,
      hide: true,
      search: true
    },
    {
      type: 'select',
      label: '所属系统',
      prop: 'appName',
      width: 150,
      span: 24,
      // dicUrl: services.adminService+'workItem/getApps',
      dicMethod: 'get',
      props: { label: 'name', value: 'name' }
    }
  ]
}

export const launchedTableSchema = {
  border: true,
  index: true,
  indexLabel: '序号',
  stripe: true,
  menuAlign: 'center',
  searchMenuSpan: 6,
  editBtn: false,
  delBtn: false,
  align: 'center',
  addBtn: false,
  menu: false,
  column: [
    {
      fixed: true,
      label: 'id',
      span: 24,
      hide: true,
      editDisabled: true,
      addDisplay: false
    },
    {
      fixed: true,
      label: '状态',
      prop: 'processStatusName',
      editDisabled: true,
      slot: true,
      width: 80,
      span: 24
    },
    {
      label: '流程标题',
      prop: 'formName',
      value: '',
      span: 24
    },
    {
      label: '待办人',
      prop: 'currentAssignees',
      width: 120,
      span: 24,
      formatter(row, value, label, column) {
        if (value === null) {
          return ''
        }
        const resultArray = []
        value.forEach(item => {
          resultArray.push(item.name)
        })
        return resultArray.join(',')
      }
    },
    {
      label: '提交时间',
      prop: 'startTime',
      value: '',
      span: 24,
      width: 135
    },
    {
      label: '总耗时',
      prop: 'totalTime',
      width: 150,
      span: 24
    },
    {
      label: '关键字',
      prop: 'keyword',
      width: 150,
      span: 24,
      search: true,
      hide: true,
      display: false,
      searchPlaceholder: '请输入名称/编码'
    },
    {
      type: 'date',
      label: '时间范围',
      prop: 'dateScope',
      width: 150,
      span: 24,
      format: 'yyyy-MM-dd HH:mm:ss',
      valueFormat: 'yyyy-MM-dd HH:mm:ss',
      searchRange: true,
      display: false,
      hide: true,
      search: true
    },
    {
      type: 'select',
      label: '所属系统',
      prop: 'appName',
      width: 150,
      span: 24,
      // dicUrl: services.adminService+'workItem/getApps',
      dicMethod: 'get',
      props: { label: 'name', value: 'name' }
    }
  ]
}

//
export const modelTableOption = {
  border: true,
  index: true,
  indexLabel: '序号',
  stripe: true,
  menuAlign: 'center',
  align: 'center',
  searchMenuSpan: 6,
  showView: true,
  viewBtn: false,
  editBtn: true,
  addBtn: false,
  delBtn: true,
  menu: true,
  menuWidth: 245,
  // expand: true,
  rowKey: 'modelKey',
  selection: false,
  column: [
    {
      label: 'id',
      span: 24,
      hide: true,
      display: false
    },
    {
      label: '流程名称',
      prop: 'name',
      slot: true,
      span: 24,
      width: 200,
      rules: [
        {
          required: true,
          message: '请输入流程名称'
        }
      ]
    },
    {
      type: 'select',
      label: '分类名称',
      prop: 'categoryCode',
      width: 100,
      align: 'left',
      dicUrl: `${services.adminService}/flowable/model/getModelCategories`,
      dicMethod: 'post',
      span: 24,
      props: {
        label: 'name',
        value: 'code'
      },
      rules: [
        {
          required: true,
          message: '请输入选择分类名称'
        }
      ]
    },
    {
      label: '流程编码',
      prop: 'modelKey',
      value: '',
      span: 24,
      editDisabled: true,
      slot: true,
      rules: [
        {
          required: true,
          message: '请输入流程编码'
        },
        {
          validator: validateModelKey,
          trigger: 'blur'
        }
      ]
    },
    {
      type: 'select',
      label: '系统',
      prop: 'appSn',
      width: 100,
      align: 'left',
      dicUrl: `${services.adminService}/flowable/model/getApps`,
      dicMethod: 'post',
      span: 24,
      props: {
        label: 'name',
        value: 'sn'
      },
      rules: [
        {
          required: true,
          message: '请选择系统'
        }
      ]
    },
    {
      label: '应用范围',
      prop: 'appliedRangeName',
      display: false,
      width: 100,
      align: 'left'
    },
    {
      label: '状态',
      prop: 'status',
      display: false,
      dicData: [
        {
          label: '草稿',
          value: 1
        },
        {
          label: '未发布',
          value: 2
        },
        {
          label: '已布发',
          value: 3
        },
        {
          label: '停用',
          value: 4
        }
      ],
      width: 70,
      align: 'center'
    },
    {
      type: 'datetime',
      format: 'yyyy-MM-dd HH:mm',
      valueFormat: 'yyyy-MM-dd HH:mm:ss',
      label: '更新时间',
      prop: 'updateTime',
      display: false,
      width: 135,
      align: 'left'
    }
  ]
}
